package com.infinova.datax.apiwriter;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.datax.common.element.Record;
import com.alibaba.datax.common.util.Configuration;
import com.alibaba.fastjson2.JSONObject;
import com.infinova.datax.common.http.HttpMethod;
import com.infinova.datax.common.http.HttpRequest;

import java.util.List;

import static com.infinova.datax.apiwriter.Config.WhereMapping.*;

/**
 * @author bigbird-0101
 * @date 2023-07-24 16:16
 */
public class Config {
    private HttpMethod method;
    private String host;
    private String protocol;
    private Integer port;
    private String requestPath;
    private List<Param> params;

    public static class Param{
        private WhereMapping whereMapping;
        private String key;

        private Object value;
        private Integer columnIndex;
        public Param() {
        }

        public Param(WhereMapping whereMapping, String key, Object value, Integer columnIndex) {
            this.whereMapping = whereMapping;
            this.key = key;
            this.value = value;
            this.columnIndex = columnIndex;
        }

        public Param(WhereMapping whereMapping, String key, Integer columnIndex) {
            this.whereMapping = whereMapping;
            this.key = key;
            this.columnIndex = columnIndex;
        }

        public WhereMapping getWhereMapping() {
            return whereMapping;
        }

        public void setWhereMapping(WhereMapping whereMapping) {
            this.whereMapping = whereMapping;
        }

        public String getKey() {
            return key;
        }

        public void setKey(String key) {
            this.key = key;
        }

        public Object getValue() {
            return value;
        }

        public void setValue(Object value) {
            this.value = value;
        }

        public Integer getColumnIndex() {
            return columnIndex;
        }

        public void setColumnIndex(Integer columnIndex) {
            this.columnIndex = columnIndex;
        }
    }


    public enum WhereMapping{
        QUERY,
        HEADER,
        BODY,
        ;
    }

    public HttpMethod getMethod() {
        return method;
    }

    public void setMethod(HttpMethod method) {
        this.method = method;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getProtocol() {
        return protocol;
    }

    public void setProtocol(String protocol) {
        this.protocol = protocol;
    }

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

    public String getRequestPath() {
        return requestPath;
    }

    public void setRequestPath(String requestPath) {
        this.requestPath = requestPath;
    }

    public List<Param> getParams() {
        return params;
    }

    public void setParams(List<Param> params) {
        this.params = params;
    }

    public static Config convert(Configuration configuration){
        String json = configuration.toJSON();
        return JSONObject.parseObject(json, Config.class);
    }

    public HttpRequest convert(Record record){
        HttpRequest httpRequest=new HttpRequest();
        httpRequest.setMethod(this.getMethod());
        String host = this.getHost();
        String protocol = ObjectUtil.defaultIfNull(this.getProtocol(),"http://");
        Integer port = this.getPort();
        String requestPath =this.getRequestPath();
        httpRequest.setUrl(protocol+host+":"+port+requestPath);
        List<Param> paramList = this.getParams();
        JSONObject body=new JSONObject();
        paramList.stream().filter(s-> BODY.equals(s.getWhereMapping()))
                .forEach(s-> setParams(record, body, s));
        httpRequest.setBody(body);
        JSONObject query=new JSONObject();
        paramList.stream().filter(s-> QUERY.equals(s.getWhereMapping()))
                .forEach(s-> setParams(record, query, s));
        httpRequest.setQuery(query);

        JSONObject header=new JSONObject();
        paramList.stream().filter(s-> HEADER.equals(s.getWhereMapping()))
                .forEach(s-> setParams(record, header, s));
        httpRequest.setHeader(header);
        return httpRequest;
    }

    private void setParams(Record record, JSONObject requestParam, Param s) {
        if(null!= s.getColumnIndex()) {
            requestParam.put(s.getKey(), record.getColumn(s.getColumnIndex()).asString());
        }else{
            if(null!= s.getValue()){
                requestParam.put(s.getKey(), s.getValue());
            }
        }
    }
}
